package com.golden.procedure.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.SingleRequest;
import com.golden.procedure.domain.WorkSchedulingSettingDO;
import com.golden.procedure.domain.WorkSiteSchedulingDO;
import com.golden.procedure.query.SchedulingQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.IWorkSiteSchedulingService;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 摄控日历
* <b>Description:</b><br> 
* @author Reading.Reagan</a>
* @version 1.0
* @Note
* <b>ProjectName:</b> procedure
* <br><b>PackageName:</b> com.golden.procedure.controller
* <br><b>ClassName:</b> SchedulingCalendarController
* <br><b>Date:</b> 2018年10月22日 下午4:12:54
 */
@RestController
@RequestMapping(value = "/api/schCalender") 
public class SchCalendarController extends BaseController {
	@Autowired
	IWorkSiteSchedulingService workSiteSchedulingService;

	public static String encodeObject(SingleResponse<WorkSchedulingSettingDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodeSitePage(PageResponse<WorkSiteSchedulingDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "摄控日历", notes = "查询上门组某月的摄控日历，有摄控返回摄控数据；无摄控，返回当天日期")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "calDate", value = "年月yyyyMM", required = true, dataType = "String") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public String queryCalendar(@RequestBody QueryRequest<SchedulingQueryDO> request) {

		// {"data":{"siteID":1,"calDate":"201810"},"version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		SchedulingQueryDO query = request.getData();

		PageResponse<WorkSiteSchedulingDO> response = new PageResponse<WorkSiteSchedulingDO>();
		ResultDO resultDO = workSiteSchedulingService.schedulingCalendar(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkSiteSchedulingDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeSitePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "设置为休息/取消休息", notes = "摄控日历中，设置某一天为休息/取消休息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "siteID", value = "景点ID", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "schedulingDateStr", value = "摄控日期(时间戳)", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isDayoff", value = "1-休息, 0-取消休息", required = true, dataType = "Integer") })
	@RequestMapping(value = { "/setDayoff.json" }, method = RequestMethod.POST)
	public String setDayoff(@RequestBody SingleRequest<WorkSiteSchedulingDO> request) {

		WorkSiteSchedulingDO settingDO = request.getData();
		settingDO.setModifiID(9);
		settingDO.setModifi("test");
		SingleResponse<WorkSchedulingSettingDO> response = new SingleResponse<WorkSchedulingSettingDO>();
		ResultDO resultDO = workSiteSchedulingService.setDayoff(settingDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

}
